<?php
namespace app\index\controller;

use \think\Controller;
//use think\facade\Request;
use think\Db;
use think\facade\Request;
use think\captcha\Captcha;
class Login extends  Controller
{
    public function index()
    {
    	if(Request::isPost())
		{
//			echo 11;exit;
			$post = input('post.');
//			print_r($post) ;exit;
			//验证验证码
			$validate = $post['code'];
//			echo $validate;exit;
			$this->validatecheck($validate);
			$dd = Db::table('shop_user')->where('user_name',$post['user_name'])->find();
			$salt = $dd['salt'];
			$passwd = md5($post['passwd'].$salt);
			$user = ['user_name'=>$post['user_name'],'passwd'=>$passwd];
//			echo $user;exit;
			$username = Db::table('shop_user')
			->where('user_name',$user['user_name'])
			->where('passwd',$user['passwd'])
			->find();
			if($username>0)
			{
				session('userinfo',$username);
				echo 12001;exit;
			}else
			{
				echo 12000;exit;
			}
		}
    	return $this->fetch();
    }
	//验证码
	public function valia()
	{
		$config =    [
			'imageH'=>40,		    // 验证码字体大小
		    'fontSize'    =>    24,    
		    // 验证码位数
		    'length'      =>    3,   
		    // 关闭验证码杂点
		    'useNoise'    =>    false, 
		];
		$captcha = new Captcha($config);
		return $captcha->entry();
	}
	
	//验证码确认
	protected function validatecheck($validate)
	{
		$captcha = new Captcha();
	
		//dump($captcha->check($validate));exit;
		if( !$captcha->check($validate))
		{
			echo 12004;exit;
		}

	}

}
